<?php

namespace App\Http\Controllers\Administrator\Game;

use App\Http\Requests\Administrator\OddTypeRequest;
use App\Models\Def\MainGamePlat;
use App\Models\OddType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OddTypeController extends Controller
{
    /**
     * gameapi
     * 2018/10/30 17:00
     * Administrator
     * index
     * Display a listing of the resource.
     * @param Request $request
     * @param OddType $oddType
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index(Request $request, OddType $oddType)
    {
        $this->authorize('index', $oddType);
        $where = [];
        $params = [
            'main_game_plat_code' => '',
        ];

        if (!empty($request->main_game_plat_code)) {
            $where[] = ['plat_code', '=', $request->main_game_plat_code];
            $params['main_game_plat_code'] = $request->main_game_plat_code;
        }

        $oddType = OddType::where($where);
        $limit = $request->limit ?? config('administrator.paginate.limit');
        $oddTypes = $oddType->paginate((integer)$limit);
        $plats = MainGamePlat::status()->get();
        return view('backend.game.odd_type.index', compact('plats', 'oddTypes', 'params', 'limit'));
    }

    /**
     * gameapi
     * 2018/10/30 17:01
     * Administrator
     * create
     * create the form for creating a new resource.
     * @param OddType $oddType
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create(OddType $oddType)
    {
        $this->authorize('create', $oddType);
        $plats = MainGamePlat::status()->get();
        return view('backend.game.odd_type.create_and_edit', compact('oddType', 'plats'));
    }

    /**
     * gameapi
     * 2018/10/30 17:02
     * Administrator
     * store
     * Store a newly created resource in storage.
     * @param OddTypeRequest $request
     * @param OddType $oddType
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(OddTypeRequest $request, OddType $oddType)
    {
        $this->authorize('create', $oddType);
        $params = array_filter($request->validated(), 'filterCallback');
        OddType::create($params);
        return redirect()->route('odd_type.index');
    }

    /**
     * gameapi
     * 2018/10/30 16:48
     * Administrator
     * show
     * Display the specified resource.
     * @param $id
     */
    public function show($id)
    {
        //
    }

    /**
     * gameapi
     * 2018/10/30 17:02
     * Administrator
     * edit
     * Show the form for editing the specified resource.
     * @param OddType $oddType
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(OddType $oddType)
    {
        $this->authorize('update', $oddType);
        $plats = MainGamePlat::status()->get();
        return view('backend.game.odd_type.create_and_edit', compact('oddType', 'plats'));
    }

    /**
     * gameapi
     * 2018/10/30 17:02
     * Administrator
     * update
     * Update the specified resource in storage.
     * @param OddTypeRequest $request
     * @param OddType $oddType
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(OddTypeRequest $request, OddType $oddType)
    {
        $this->authorize('update', $oddType);
        $params = array_filter($request->validated(), 'filterCallback');
        $oddType->update($params);
        return redirect()->route('odd_type.index');
    }

    /**
     * gameapi
     * 2018/10/30 17:03
     * Administrator
     * destroy
     * Remove the specified resource from storage.
     * @param Request $request
     * @param OddType $oddType
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function destroy(Request $request, OddType $oddType)
    {
        $this->authorize('destroy', $oddType);
        $delIds = json_decode($request->getContent(), true);
        $delRes = OddType::whereIn('id', $delIds)->delete();
        if ($delRes) {
            return response()->json([
                'message' => '删除成功！受影响行数：' . $delRes,
            ], 200
            );

        } else {
            return response()->json([
                'message' => '删除失败！',
            ], 500
            );
        }
    }
}
